module ActionControllerAutocompleteExtension
  
  def get_value object, o 
    case object
      when Proc then object.call(o)
      when String then o.send(object)        
    end
  end
  
  def generate_array_for_autocomplete array, labeling
    result = "["
    array.each do |o|  
      result += "{\"id\": \"#{get_value(labeling[:id], o)}\", 
                  \"label\": \"#{get_value(labeling[:label], o)}\", 
                  \"value\": \"#{get_value(labeling[:value], o)}\"},"
    end
    result = result.chop
    result += "]"   
  end

end